/**
 * WorkOrderServiceImpl.java
 * Created at 2017-11-22
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.message.model.SysMessage;
import com.broadtext.message.service.IMessageService;
import com.broadtext.rms.system.model.TmWoFile;
import com.broadtext.rms.system.model.TmWoHistory;
import com.broadtext.rms.system.model.TmWorkOrder;
import com.broadtext.rms.system.service.IWorkOrderService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: WorkOrderServiceImpl</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-11-22</p>
 */
@Service("workOrderService")
public class WorkOrderServiceImpl extends EntityServiceImpl<TmWorkOrder> implements IWorkOrderService{
    
    @Autowired
    @Qualifier("messageService")
    IMessageService messageService;
    
    @Autowired
    @Qualifier("workOrderDao")
    public void setEntityDao(EntityDao<TmWorkOrder> entityDao) {
        super.entityDao = entityDao;
    }

    /** (non-Javadoc)
     * <p>Title: getWorkOrderHistory</p>
     * <p>Description: </p>
     * @param map
     * @param pageBounds
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IWorkOrderService#getWorkOrderHistory(java.util.Map, com.github.miemiedev.mybatis.paginator.domain.PageBounds)
     */
    @Override
    public EntityPageBean<Object> getWorkOrderHistory(Map<String, Object> map, PageBounds pageBounds)
            throws ServiceException {
        try {
            return this.entityDao.findObjectEntityPage("com.broadtext.rms.system.model.TmWoHistory.getWorkOrderHistory", map, pageBounds);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** (non-Javadoc)
     * <p>Title: getWordOrderDetail</p>
     * <p>Description: </p>
     * @param woId
     * @param woType
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IWorkOrderService#getWordOrderDetail(java.lang.String, java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> getWordOrderDetail(String woId, String woType) throws ServiceException {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("woId", woId);
        queryMap.put("woType", woType);
        try {
            List<Map<String,Object>> list = this.entityDao.findByProperty("com.broadtext.rms.system.model.TmWorkOrder.getWordOrderDetail", queryMap);
            if(list!=null&&list.size()>0) {
                return list.get(0);
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** (non-Javadoc)
     * <p>Title: updateWoStatus</p>
     * <p>Description: </p>
     * @param woId 工单id
     * @param woStatus 工单状态
     * @param woType 工单类型
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IWorkOrderService#updateWoStatus(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public void updateWoStatus(String woId, String woStatus, String woType, String updateUser) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>();
        //当前时间
        Date now =new Date();
        params.put("woId", woId);
        params.put("woStatus", woStatus);
        params.put("updateUser", updateUser);
        params.put("updateDate", now);
        params.put("woType", woType);
        try {
            //更改状态
            this.entityDao.updateRecord("com.broadtext.rms.system.model.TmWorkOrder.updateWoStatus", params);
            //更新工单
            TmWorkOrder wo = new TmWorkOrder();
            wo.setPkId(woId);
            wo.setUpdateDate(now);
            wo.setUpdateUser(updateUser);
            this.entityDao.updateRecord("com.broadtext.rms.system.model.TmWorkOrder.updateRecord", wo);
            //查询用户名
            String loginName = updateUser;
            String operator = "";
            List<Map<String,Object>> list = this.entityDao.findByProperty("com.broadtext.rms.system.model.TmServicePlan.getUserNamebyLoginName", loginName);
            if(list!=null&&list.size()>0){
                operator = String.valueOf(list.get(0).get("username"));
            }
            //插入工单操作历史数据
            TmWoHistory woHistory = new TmWoHistory();
            woHistory.setPkId(UUIDUtils.getUUID());
            woHistory.setWoId(woId);
            woHistory.setOperatorName(updateUser);
            woHistory.setOperator(operator);
            woHistory.setOperatingDate(now);
            woHistory.setWoStatus(woStatus);
            woHistory.setDelFlag("0");
            this.entityDao.insertRecord("com.broadtext.rms.system.model.TmWoHistory.insertRecord", woHistory);
            String receiver = this.getReceiver(woId, woType);
            //推送通知  服务申请状态被更新
            if("1".equals(woType)) {
                SysMessage message = new SysMessage();
                message.setSender("system");
                message.setReceiver(receiver);
                message.setDelFlag("0");
                message.setMessageTitle("您的服务申请有了更新，请注意查看");
                //拼装消息内容
                StringBuffer content = new StringBuffer("您的服务申请“");
                Map<String, Object> serviceReq = this.getWordOrderDetail(woId, woType);
                content.append(serviceReq.get("woContent"));
                content.append("” 状态更新为");
                if("1".equals(woStatus)) {
                    content.append("未处理");
                } else if ("2".equals(woStatus)) {
                    content.append("已确认");
                } else if ("3".equals(woStatus)) {
                    content.append("处理中");
                } else if ("4".equals(woStatus)) {
                    content.append("已解决");
                } else if ("5".equals(woStatus)) {
                    content.append("已关闭");
                }
                content.append("，您可以在“运营售后管理--服务工单”查看详情。");
                
                message.setMessageContent(content.toString());
                message.setMessageStatus("0");//未读
                message.setCreateUser(updateUser);
                message.setPushDate(now);
                message.setCreateDate(now);
                message.setMessageType("2");//服务动态
                this.messageService.insertMessage(message);
            }
            
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /** (non-Javadoc)
     * <p>Title: saveFile</p>
     * <p>Description: </p>
     * @param woFile
     * @throws Exception
     * @see com.broadtext.rms.system.service.IWorkOrderService#saveFile(com.broadtext.rms.system.model.TmWoFile)
     */
    @Override
    public void saveFile(TmWoFile woFile) throws ServiceException {
        try {
            this.entityDao.insertRecord("com.broadtext.rms.system.model.TmWoFile.insertRecord", woFile);
        } catch (DaoException e) {
            e.printStackTrace();
        }
    }

    /** (non-Javadoc)
     * <p>Title: getWorkOrderFile</p>
     * <p>Description: </p>
     * @param map
     * @param pageBounds
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IWorkOrderService#getWorkOrderFile(java.util.Map, com.github.miemiedev.mybatis.paginator.domain.PageBounds)
     */
    @Override
    public EntityPageBean<Object> getWorkOrderFile(Map<String, Object> map, PageBounds pageBounds)
            throws ServiceException {
        try {
            return this.entityDao.findObjectEntityPage("com.broadtext.rms.system.model.TmWoFile.getWorkOrderFile", map, pageBounds);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** (non-Javadoc)
     * <p>Title: getWoFileById</p>
     * <p>Description: </p>
     * @param pkId
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IWorkOrderService#getWoFileById(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public TmWoFile getWoFileById(String pkId) throws ServiceException {
        try {
            List<TmWoFile> list = this.entityDao.findByProperty("com.broadtext.rms.system.model.TmWoFile.getWoFileById", pkId);
            if(list!=null&&list.size()>0){
                return list.get(0);
            }
        } catch (DaoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /** (non-Javadoc)
     * <p>Title: deleteFile</p>
     * <p>Description: </p>
     * @param woFile
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IWorkOrderService#deleteFile(com.broadtext.rms.system.model.TmWoFile)
     */
    @Override
    public void deleteFile(TmWoFile woFile) throws ServiceException {
        try {
            this.entityDao.updateRecord("com.broadtext.rms.system.model.TmWoFile.updateRecord", woFile);
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }
    
    @SuppressWarnings("unchecked")
    private String getReceiver(String woId,String woType) throws DaoException {
        List<String> receivers = new ArrayList<String>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("woId", woId);
        params.put("woType", woType);
        receivers = this.entityDao.findByProperty("com.broadtext.message.model.SysMessage.getReceiver", params);
        if(receivers!=null&&receivers.size()>0&&receivers.get(0)!=null) {
            return receivers.get(0);
        }
        return null;
    }
    
    

}
